package com.cskaoyan.controller;

import com.cskaoyan.bean.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @author stone
 * @date 2022/09/01 17:47
 */
@RestController
@RequestMapping("user")
public class UserController {

    /*@RequestMapping("register1")
    public BaseRespVo register1(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String age = request.getParameter("age");
        int i = Integer.parseInt(age);
        return BaseRespVo.ok();
    }*/

    @RequestMapping("transfer")
    public BaseRespVo transfer(Integer fromId, Integer destId, Integer money) {
        //accountService.transfer(fromId,destId,money)
        return BaseRespVo.ok();
    }

    @RequestMapping("register1") // user/register1
    public BaseRespVo register1(String username,String password,Integer age,String birthday) throws ParseException {
        Date date = new SimpleDateFormat("yyyy/MM/dd").parse(birthday);
        return BaseRespVo.ok();
    }

    @RequestMapping("register2")
    public BaseRespVo register2(String username,String password,int age) throws ParseException {
        return BaseRespVo.ok();
    }

    @RequestMapping("register3")
    public BaseRespVo register3(String username,String password,Integer age,
                                Date birthday)  {
        return BaseRespVo.ok();
    }

    @RequestMapping("register4")
    public BaseRespVo register4(String username,String password,Integer age,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday)  {
        return BaseRespVo.ok();
    }

    @RequestMapping("register5")
    public BaseRespVo register5(String username,String password,Integer age,
                                String[] hobbies,
                                Integer[] ids)  {
        System.out.println(Arrays.asList(hobbies));
        System.out.println(Arrays.asList(ids));
        return BaseRespVo.ok();
    }

    // 要使用引用类型对象来接收

    /*@RequestMapping("register6")
    public BaseRespVo register6(String username,String password,Integer age,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday,
                                String[] hobbies,
                                Integer[] ids)  {
        System.out.println(Arrays.asList(hobbies));
        System.out.println(Arrays.asList(ids));
        return BaseRespVo.ok();
    }*/
    @RequestMapping("register6")
    public BaseRespVo register6(User user)  {
        System.out.println(user);
        return BaseRespVo.ok();
    }

    // 混合接收 localhost:8080/user/query?age=20&page=1&limit=10
    /*@RequestMapping("query")
    public BaseRespVo query(Integer age, PageInfo pageInfo) {
        return BaseRespVo.ok();
    }*/

    // 在改造一个写法
    @RequestMapping("query")
    public BaseRespVo query(AgeInfo ageInfo) {
        return BaseRespVo.ok();
    }


    // 接收Json
    @RequestMapping("login")
    public BaseRespVo login(@RequestBody String value) {
        return BaseRespVo.ok();
    }
    // 接收Json
    @RequestMapping("login2")
    public BaseRespVo login2(@RequestBody JsonUser jsonUser) {
        BaseRespVo ok = BaseRespVo.ok();
        ok.setData(jsonUser);
        return ok;
    }
    // 接收Json
    @RequestMapping("login3")
    public BaseRespVo login3(@RequestBody Map map) {//LinkedHashMap
        return BaseRespVo.ok();
    }
}
